// 本模块为首页视图相关逻辑及状态,主要用来做页面传参
import login from '@/api/login.js'
import vue from 'vue'
import {
  Message
} from 'element-ui'
const state = {
  isLogin: false,
  token: '',
}

const getters = {
  isLogin(state) {
    return state.isLogin
  },
  token(state) {
    return state.token
  },
}

const mutations = {
  setLoginState(state, {
    key,
    value
  }) {
    state[key] = value
  },
}
const actions = {
  sendSms({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      login.sendSms({
        mobile: data.mobile,
        type: data.type,
      }).then((res) => {
        let code = res.data.code;
        if (code == 200) {
          resolve(res)
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
  register({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      login.register({
        phone: data.phone,
        password: data.password,
        name: data.name,
        phone_code: data.phone_code,
        email: data.email,
      }).then((res) => {
        let code = res.data.code;
        if (code == 200) {
          resolve(res)
        } else {
          Message.error(res.data.message);
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
  passwordCodeLogin({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      login.passwordCodeLogin({
        phone: data.phone,
        password: data.password,
        code: data.code
      }).then((res) => {
        let code = res.data.code;
        if (code == 200) {
          commit('setLoginState', {
              key: 'isLogin',
              value: true
            }),
            commit('setLoginState', {
              key: 'token',
              value: res.data.data.token
            }),
            localStorage.setItem('token', res.data.data.token);
          localStorage.setItem('password_status', res.data.data.password_status);
          resolve(res)
        } else {
          Message.error(res.data.message);
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
  passwordLogin({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      login.login({
        phone: data.phone,
        password: data.password,
      }).then((res) => {
        let code = res.data.code;
        if (code == 200) {
          commit('setLoginState', {
              key: 'isLogin',
              value: true
            }),
            commit('setLoginState', {
              key: 'token',
              value: res.data.data.token
            }),
            localStorage.setItem('token', res.data.data.token);
          resolve(res)
        } else {
          Message.error(res.data.message);
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
  codeLogin({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      login.codeLogin({
        phone: data.phone,
        phone_code: data.phone_code,
      }).then((res) => {
        let code = res.data.code;
        if (code == 200) {
          commit('setLoginState', {
              key: 'isLogin',
              value: true
            }),
            commit('setLoginState', {
              key: 'token',
              value: res.data.data.token
            }),
            localStorage.setItem('token', res.data.data.token);
          resolve(res)
        } else {
          Message.error(res.data.message);
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
  codeChangePwd({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      login.codeChangePwd({
        phone: data.phone,
        new_password: data.new_password,
        phone_code: data.phone_code,
      }).then((res) => {
        let code = res.data.code;
        if (code == 200) {
          resolve(res)
        } else {
          Message.error(res.data.message);
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
  clearLogin({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      login.logOut().then((res) => {
        let code = res.data.code;
        if (code == 200) {
          commit('setLoginState', {
            key: 'token',
            value: ''
          })
          commit('setLoginState', {
              key: 'isLogin',
              value: false
            }),
            localStorage.removeItem('token');
          localStorage.removeItem('password_status');
          localStorage.removeItem('password_status_message');
          Message.error(res.data.message);
          resolve(res)
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
}
export default {
  state,
  getters,
  actions,
  mutations
}